﻿namespace UFIDA.U9.CUST.AITBarCodeBP
{
	using System;
	using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using UFIDA.U9.Base;
    using UFIDA.U9.CUST.AITBarCodeBE;
    using UFIDA.U9.CUST.AITBarCodeBP.utils;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.DataAccess;

    /// <summary>
    /// PrintBarCode partial 
    /// </summary>	
    public partial class PrintBarCode 
	{	
		internal BaseStrategy Select()
		{
			return new PrintBarCodeImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class PrintBarCodeImpementStrategy : BaseStrategy
	{
		public PrintBarCodeImpementStrategy() { }

		public override object Do(object obj)
		{						
			PrintBarCode bpObj = (PrintBarCode)obj;

			if (bpObj.IDS == null || bpObj.IDS.Count == 0)
				throw new Exception("传入IDS为空");
			if (string.IsNullOrEmpty(bpObj.PrintTemplateID))
				throw new Exception("PrintTemplateID为空");

			//更新条码
			UpdateBarCode bp = new UpdateBarCode();
			bp.IDS = bpObj.IDS;
			bp.Do();

			//修改BE打印信息
			using(ISession session = Session.Open())
			{
                foreach (var id in bpObj.IDS)
                {
                    AITBarCode be = AITBarCode.Finder.FindByID(id);
                    if (be == null) throw new Exception("查找实体[AITBarCode]失败!");
                    be.PrintNum++;
                    be.PrintTime = DateTime.Now;
                    be.Printer = Context.LoginUser;
                }

				session.Commit();
            }


            //构造打印ds
            BarcodeRuleConfiguration config = BarCodeUtils.LoadBarcodeRuleConfig();
            PrintRule printRule = BarCodeUtils.GetPrintRule(bpObj.PrintTemplateID,config);
            DataSet ds=new DataSet();
			string idstr=string.Join(",", bpObj.IDS);
            string sql = $"select * from {printRule.erpView} where ID in ({idstr})";
            DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);

			//处理返回
            PrintAITBarCodeDTO retdto = new PrintAITBarCodeDTO();
			retdto.Data = ds;
			return retdto;
        }		
	}

	#endregion
	
	
}